//
//  ChangePasswordViewController.swift
//  LongTermInsurance
//
//  Created by zolobdz on 2018/11/23.
//  Copyright © 2018年 HeChao. All rights reserved.
//

import UIKit
import RxSwift
class ChangePasswordViewController: BaseViewController {
    
    @IBOutlet weak var originTF: UITextField!
    
    @IBOutlet weak var newTF: UITextField!
    
    @IBOutlet weak var modifyButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "修改密码"
        #if DEBUG
        
        originTF.text = "170517"
        
        #endif
    }
    
    deinit {
        print("")
    }
    
    @IBAction func modifyButtonAction(_ sender: UIButton) {
        
        guard let oldString = originTF.text, oldString.count > 0 else {
            makeToast(originTF.placeholder ?? "")
            return
        }
        
        guard let newString = newTF.text, newString.count > 0 else {
            makeToast(newTF.placeholder ?? "")
            return
        }
        
        if newString.count < 6 {
            CDUtility.showMSG(message: "密码至少6位")
            return
        }
        
        let values = ["APPLB": "1",
                      "UserCode": MyselfSingleton.shared.userInfo?.personNum ?? "",
                      "OldPassWord": oldString,
                      "NewPassWord": newString].convertToJsonString()
        
        let paramString = "<json>"+values+"</json>"
        
        NetworkingHandle.fetchNetworkData(method: "upDatePassWord", paramString: paramString, success: {[weak self] (dict) in
            if let code = dict["ResultCode"] as? Int,code == 0 {
                CDUtility.showMSG(message: "修改成功")
                _ = Observable<Int>.interval(1, scheduler: MainScheduler.instance).delay(0.5, scheduler: MainScheduler.instance).subscribe(onNext: { [weak self](t) in

                    self?.navigationController?.popViewController(animated: true)
                }, onError: nil, onCompleted: nil, onDisposed: nil)
                return
            }
            if let code = dict["ResultCode"] as? Int,code == 1 {
                if let msgString = dict["ResultMessage"] as? String {
                    CDUtility.showMSG(message: msgString)
                }
            }
        }) {
            print("")
        }
        
    }
    
    
    
    


}
